package online.heycm.rbac.service.basic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import online.heycm.platform.common.entity.result.Optional;
import online.heycm.platform.common.entity.result.PageResult;
import online.heycm.rbac.common.cache.DictionaryCache;
import online.heycm.rbac.common.constant.ErrorCode;
import online.heycm.rbac.common.converter.ConvertHelper;
import online.heycm.rbac.common.converter.RbacConverter;
import online.heycm.rbac.common.utils.PageHelper;
import online.heycm.rbac.domain.basic.repository.DictionaryRepository;
import online.heycm.rbac.domain.basic.repository.entity.BasicDictionary;
import online.heycm.rbac.param.basic.DictionaryAddReq;
import online.heycm.rbac.param.basic.DictionaryEditReq;
import online.heycm.rbac.param.basic.DictionaryPageReq;
import online.heycm.rbac.param.basic.DictionaryRes;
import online.heycm.rbac.service.basic.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 字典业务实现
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/12 9:26
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Autowired
    private DictionaryCache dictionaryCache;

    @Override
    public Optional addDictionary(DictionaryAddReq req) {
        BasicDictionary dictionary = RbacConverter.INST.convert(req);
        boolean save = dictionaryRepository.save(dictionary);
        if (save) {
            dictionaryCache.update(dictionary);
        }
        return Optional.ifFail(save, ErrorCode.DICT_INSERT);
    }

    @Override
    public Optional editDictionary(DictionaryEditReq req) {
        BasicDictionary dictionary = RbacConverter.INST.convert(req);
        boolean save = dictionaryRepository.updateById(dictionary);
        if (save) {
            dictionaryCache.update(dictionary);
        }
        return Optional.ifFail(save, ErrorCode.DICT_UPDATE);
    }

    @Override
    public Optional<DictionaryRes> queryDetail(Integer dictId) {
        BasicDictionary dictionary = dictionaryRepository.getById(dictId);
        if (dictionary == null) {
            return Optional.error(ErrorCode.DICT_NOT_EXIST);
        }
        return Optional.of(RbacConverter.INST.convert(dictionary));
    }

    @Override
    public Optional<PageResult<DictionaryRes>> pageDictionary(DictionaryPageReq req) {
        IPage<BasicDictionary> page = PageHelper.getPage(req);
        LambdaQueryWrapper<BasicDictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(req.getDicAlias()), BasicDictionary::getDicAlias, req.getDicAlias());
        wrapper.eq(StringUtils.hasText(req.getDicTable()), BasicDictionary::getDicTable, req.getDicTable());
        wrapper.eq(StringUtils.hasText(req.getDicValue()), BasicDictionary::getDicValue, req.getDicValue());
        wrapper.eq(StringUtils.hasText(req.getDicEnum()), BasicDictionary::getDicEnum, req.getDicEnum());
        wrapper.eq(StringUtils.hasText(req.getRemark()), BasicDictionary::getRemark, req.getRemark());
        dictionaryRepository.page(page, wrapper);
        PageResult<DictionaryRes> pageResult = ConvertHelper.page(page, RbacConverter.INST::convert);
        return Optional.of(pageResult);
    }

    @Override
    public Optional<List<DictionaryRes>> listDictionary(String dicAlias) {
        LambdaQueryWrapper<BasicDictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BasicDictionary::getDicAlias, dicAlias);
        List<BasicDictionary> dictionaries = dictionaryRepository.list(wrapper);
        List<DictionaryRes> converted = ConvertHelper.list(dictionaries, RbacConverter.INST::convert);
        return Optional.of(converted);
    }
}
